<template>
	<view class="page">
		<view class="login-container">
            <view class="logo">
                <image title="logo" src="/static/logo.png" style="width: 90px; height: 90px;" mode="aspectFit"></image>
            </view>
			<view class="title-wrapper">
				<view class="welcome-tips">
					<view class="tips-text">欢迎使用，<br>维思顿青少年脊柱侧弯筛查系统</view>
				</view>
			</view>
			<uni-forms ref="form" :modelValue="loginForm" :rules="rules" class="login-form">
				<uni-forms-item label="" name="username">
					<uni-easyinput prefixIcon="person" v-model="loginForm.username" placeholder="请输入账号" />
				</uni-forms-item>
				<uni-forms-item label="" name="password">
					<uni-easyinput prefixIcon="locked" v-model="loginForm.password" type="password" placeholder="请输入密码"/>
				</uni-forms-item>
                <!-- <uni-forms-item label="" name="code">
                	<view class="code-wrapper">
                		<uni-easyinput v-model="loginForm.code" type="number" placeholder="请输入验证码" />
                		<image :src="codeUrl" @click="getCode" mode="aspectFit" style="width: 180rpx;height: 90rpx;"></image>
                	</view>
                </uni-forms-item> -->
			</uni-forms>
			<button class="login-btn" type="primary" @click="submit('valiForm')">登录</button>
		</view>
	</view>
</template>

<script>
	import { login } from "@/api/login";
	import { setToken } from "@/utils/auth.js"
	export default {
		data() {
			return {
				loginForm: {
					username: '' || uni.getStorageSync('wsd-username'),
					password: '' || uni.getStorageSync('wsd-password'),
					uuid: ''
				},
				rules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '请输入账号',
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入密码',
						}]
					}
				}
			}
		},
		methods: {
			submit(){
				this.$refs.form.validate().then(res=>{
					// console.log(this.loginForm)
					login(this.loginForm).then(res => {
						// console.log(res)
						if(res.code == 200) {
							//保存token到本地
							setToken(res.token)
                            
                            //自动记录用户名密码
                            uni.setStorageSync('wsd-username', this.loginForm.username);
                            uni.setStorageSync('wsd-password', this.loginForm.password);
                            
                            //同时拉取用户信息，保存用户权限到本地
                            // getInfo().then(res => {
                            //     uni.setStorageSync('wsd-permissions', JSON.stringify(res.permissions));
                            // })
                            
							//跳转首页
							uni.redirectTo({
							    url: '/pages/index/index'
							})
						}
					})
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>

<style lang="less">
@import url('login.less');
</style>
